<?	/*
	// File:	transfer.inc.phps
	// Purpose:	Deal with the transfer table
	// Author:	Felix <webmaster@can-host.com>
	*/

// Sort new http entries
$lasttime = 0;
$thishour = date("Ymdh")."0000";
unset($newdata);
$dbp = mysql_query("SELECT hostname, bytesin, bytesout, hits, time FROM transfer WHERE type = 'http' AND frequency = 'now' ORDER BY hostname,time DESC");
while ($data = mysql_fetch_array($dbp)) {
	
	if ($newdata['hostname'] != $data['hostname']) {
		// find corresponding ID
		unset($newid);
		if (ereg($rx['ip'],$data['hostname'])) {
			$dbp1 = mysql_query("SELECT id FROM domains WHERE ip = '".$data['hostname']."' AND priority = 0");
			$tmp = mysql_fetch_array($dbp1);
			if ($tmp AND mysql_num_rows($dbp1) < 2)
				$newid = $tmp['id'];
			else {
				$newid = 0;
				webcp_log(1,0,"cron","Unknown Host: ".$data['hostname']." KB IN: ".round($data['bytesin']/1024)." KB OUT: ".round($data['bytesout']/1024)." HITS: ".$data['hits'],0);
			}
		}
		elseif (ereg($rx['dom'],$data['hostname'])) {
			$tmp = explode(".",$data['hostname']);
			$host = array_shift($tmp);
			$domain = implode(".",$tmp);
			$dbp1 = mysql_query("SELECT id FROM domains WHERE host = '$host' AND domain = '$domain'");
			$tmp = mysql_fetch_array($dbp1);
			if ($tmp AND mysql_num_rows($dbp1) < 2)
				$newid = $tmp['id'];
			else {
				$dbp1 = mysql_query("SELECT id FROM domains WHERE aliases LIKE '% $host %' AND domain = '$domain'");
				if ($tmp AND mysql_num_rows($dbp1) < 2)
					$newid = $tmp['id'];
				else {
					$newid = 0;
					webcp_log(1,0,"cron","Unknown Host: ".$data['hostname']." KB IN: ".round($data['bytesin']/1024)." KB OUT: ".round($data['bytesout']/1024)." HITS: ".$data['hits'],0);
				}
			}
		}
		else {
			$newid = 0;
			webcp_log(1,0,"cron","Unknown Host: ".$data['hostname']." KB IN: ".round($data['bytesin']/1024)." KB OUT: ".round($data['bytesout']/1024)." HITS: ".$data['hits'],0);
			$data['hostname'] = addslashes($data['hostname']);
		}
		
	}
	
	if ($lasttime < $data['time'])
		$lasttime = $data['time'];
	
	if ($newdata['hostname'] == $data['hostname']) {
		$newdata['bytesin'] += $data['bytesin'];
		$newdata['bytesout'] += $data['bytesout'];
		$newdata['hits'] += $data['hits'];
		if ($newdata['time'] < $data['time'])
			$newdata['time'] = $data['time'];
	}
	else {
		if ($newdata) {
			// check if hourly entry exists already & update db
			$dbp1 = mysql_query("SELECT id FROM transfer WHERE id = $newid AND time >= $thishour");
			if (!mysql_num_rows($dbp1))
				mysql_query("INSERT INTO transfer SET id = $newid, type = 'http', bytesin = '".$newdata['bytesin']."', bytesout = '".$newdata['bytesout']."', hits = '".$newdata['hits']."', frequency = 'hourly', time = ".$newdata['time']);
			else
				mysql_query("UPDATE transfer SET bytesin = bytesin+'".$newdata['bytesin']."', bytesout = bytesout+'".$newdata['bytesout']."', hits = hits+'".$newdata['hits']."', time = ".$newdata['time']." WHERE id = $newid AND time >= $thishour");
		}
		$newdata = $data;
	}
}

// set last entry
if ($newdata) {
	// check if hourly entry exists already & update db
	$dbp1 = mysql_query("SELECT id FROM transfer WHERE id = $newid AND time >= $thishour");
	if (!mysql_num_rows($dbp1))
		mysql_query("INSERT INTO transfer SET id = $newid, type = 'http', bytesin = '".$newdata['bytesin']."', bytesout = '".$newdata['bytesout']."', hits = '".$newdata['hits']."', frequency = 'hourly', time = ".$newdata['time']);
	else
		mysql_query("UPDATE transfer SET bytesin = bytesin+'".$newdata['bytesin']."', bytesout = bytesout+'".$newdata['bytesout']."', hits = hits+'".$newdata['hits']."', time = ".$newdata['time']." WHERE id = $newid AND time >= $thishour");
}


// remove entries we checked
mysql_query("DELETE FROM transfer WHERE frequency = 'now' AND time <= '$lasttime'");
/*
// make daily & monthly entries
// daily
$lasttime = 0;
if ($minute == 1 AND $hour == 0) {
	$today = date("Ymd")."000000";
	$dbp = mysql_query("SELECT * FROM transfer WHERE type = 'http' AND frequency = 'hourly' AND time < '$today' ORDER BY id,time DESC");
	while ($data = mysql_fetch_array($dbp)) {
		if ($lasttime < $data['time'])
			$lasttime = $data['time'];
		$sql_query = "INSERT INTO transfer SET id = ".$data['id'].", type = 'http', bytesin = '".$data['bytesin']."', bytesout = '".$data['bytesout']."', hits = '".$data['hits']."', frequency = 'daily', time = '".$data['time']."'";
		
		mysql_query($sql_query);
	}
}
// remove entries we checked
mysql_query("DELETE FROM transfer WHERE frequency = 'hourly' AND time <= '$lasttime'");
*/
/*
// monthly
$lasttime = 0;
if ($minute == 5 AND $hour == 0 AND $day == 1) {
	$today = date("Ym")."00000000";
	$dbp = mysql_query("SELECT SUM(bytesin) AS bytesin, SUM(bytesout) AS bytesout, SUM(hits) AS hits, id, time FROM transfer WHERE type = 'http' AND frequency = 'daily' AND time < '$today' GROUP BY id ORDER BY id,time DESC");
	while ($data = mysql_fetch_array($dbp)) {
		print_r($data);
		if ($lasttime < $data['time'])
			$lasttime = $data['time'];
		$sql_query = "INSERT INTO transfer SET id = ".$data['id'].", type = 'http', bytesin = '".$data['bytesin']."', bytesout = '".$data['bytesout']."', hits = '".$data['hits']."', frequency = 'monthly', time = '".$data['time']."'";
		mysql_query($sql_query);
	}
}
// remove entries we checked
mysql_query("DELETE FROM transfer WHERE frequency = 'daily' AND time <= '$lasttime'");
/*
?>
